package db_servicer

/*
var ses *ora.Ses
var logger *log.Logger

*/
// 应该将报错信息 的生成，集中在一处，最佳位置是数据库存贮过程
// 故，建议返回值结构增加一项：　ｆａｌｇ　ｉｎｔ８，　ｍｓｇ　ｓｔｒｉｎｇ，　ｄａｔａ　　ｓｔｒｉｎｇ

func New_Device(v_device_id, v_device_type, v_SIM string, v_pond_id int) dev_retMsg {

	var v_flag int8
	var v_ret_msg, v_data_msg string
	// logger = log.New(os.Stdout, ">>>>>>>DB.New Device:", log.LstdFlags|log.Lshortfile)

	if _, err := ses.PrepAndExe("CALL pkg_device_info.new_device(:p1,:p2,:p3,:p4,:p5, :p6, :p7 )",
		&v_flag, &v_ret_msg, &v_data_msg, v_device_id, v_device_type, v_SIM, int32(v_pond_id)); err != nil {

		logger.Panicf("***Panic in New_Device: %v ***\n", err)
	}

	logger.Println("\n>>>device_id:", v_device_id, ",flag:", v_flag, ",Msg:", v_ret_msg, "Data:", v_data_msg)

	return dev_retMsg{Flag: v_flag,
		Message: v_ret_msg,
		Data:    v_data_msg,
	}

}

func Device_List(p_user_id int) dev_retMsg {

	var v_flag int8
	var v_ret_msg, v_data_msg string
	// logger = log.New(os.Stdout, "DB.New Device:", log.LstdFlags|log.Lshortfile)

	if _, err := ses.PrepAndExe("CALL pkg_device_info.device_list(:p1,:p2 , :p3, :p4 )",
		&v_flag, &v_ret_msg, &v_data_msg, int32(p_user_id)); err != nil {

		logger.Panicf("***Panic in Device_List : %v ***\n", err)
	}

	//	logger.Println("user_id:", p_user_id , ",flag:", v_flag,  ",Msg:", v_ret_msg, "Data:", v_data_msg)

	return dev_retMsg{Flag: v_flag,
		Message: v_ret_msg,
		Data:    v_data_msg,
	}

}

func Modify_Device(p_user_id, pond_id, o_pond_id int,
	device_id, device_type, o_device_type, SIM, o_SIM, status, o_status string) dev_retMsg {

	var v_flag int8
	var v_ret_msg, v_data_msg string

	// logger = log.New(os.Stdout, ">>>>>>>DB.Modify Device:", log.LstdFlags|log.Lshortfile)

	if _, err := ses.PrepAndExe("CALL pkg_device_info.modify_device(:p1,:p2 , :p3, :p4 , :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12, :p13 )",
		&v_flag, &v_ret_msg, &v_data_msg, int32(p_user_id), int32(pond_id), int32(o_pond_id),
		device_id, device_type, o_device_type, SIM, o_SIM, status, o_status); err != nil {

		logger.Panicf("***Panic in Modify_Device: %v ***\n", err)
	}

	// logger.Println("user_id:", p_user_id , ",flag:", v_flag,  ",Msg:", v_ret_msg, "Data:", v_data_msg)
	// logger.Println( "'\ndevice_id    :'",device_id    ,
	// 				"\npond_id      :'",pond_id      ,
	// 				"',o_pond_id    :'",o_pond_id    ,
	// 				"'\ndevice_type  :'",device_type  ,
	// 				"',o_device_type:'",o_device_type,
	// 				"'\nSIM          :'",SIM          ,
	// 				"',o_SIM        :'",o_SIM        ,
	// 				"'\nstatus       :'",status       ,
	// 				"',o_status     :'",o_status     , "'")

	return dev_retMsg{Flag: v_flag,
		Message: v_ret_msg,
		Data:    v_data_msg,
	}

}
